#pragma once
#include <mutex>
#include <condition_variable>



class Semaphore
{
public:
	Semaphore(int limit = 0) 
		:_resLimit(limit)
	{}

	~Semaphore() = default;

	void post()
	{
		std::unique_lock<std::mutex> lock(_mtx);
		_resLimit++;
		_cond.notify_all();
	}

	void wait()
	{
		std::unique_lock<std::mutex> lock(_mtx);
		_cond.wait(lock, [&]()->bool {
			return _resLimit > 0;
			});

		_resLimit--;
	}
	
private:
	int _resLimit;
	std::mutex _mtx;
	std::condition_variable _cond;

};
